//
//  WodejinengTableViewCell.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/15.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "WodejinengTableViewCell.h"

@interface WodejinengTableViewCell ()
{
    UILabel * titleLabel;
    UIButton * deleteButton;
    UIButton * sellableButton;
    
    NSDictionary * cdata;
    BOOL csold;
}
@end

@implementation WodejinengTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth - 70, 20)];
        titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:titleLabel];
        
        deleteButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth - 70, 10, 60, 20)];
        deleteButton.layer.cornerRadius = 10;
        deleteButton.titleLabel.font = [UIFont systemFontOfSize:13];
        deleteButton.layer.borderColor = themeColor.CGColor;
        deleteButton.layer.borderWidth = 1;
        [deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [deleteButton setTitleColor:themeColor forState:UIControlStateNormal];
        [deleteButton addTarget:self action:@selector(deleteAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:deleteButton];
        
        sellableButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth - 70 - 70, 10, 60, 20)];
        sellableButton.titleLabel.font = [UIFont systemFontOfSize:13];
        sellableButton.layer.cornerRadius = 10;
        [sellableButton addTarget:self action:@selector(sellableToggle:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:sellableButton];
        
        UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 39, ScreenWidth, 1)];
        line.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
        [self addSubview:line];
    }
    return self;
}

+(CGFloat)height
{
    return 40;
}

-(void)deleteAction
{
    [self.delegate deleteCurrent:cdata];
}

-(void)sellableToggle:(UIButton *)button
{
    if (csold) {
        [self.delegate removeAbility:cdata];
    }
    else
    {
        [self.delegate addAbility:cdata];
    }
}

-(void)loadData:(NSDictionary *)data sold:(BOOL)sold
{
    cdata = data;
    csold = sold;
    titleLabel.text = [data objectForKey:@"Name"];
    if (sold)
    {
        [sellableButton setTitle:@"可售卖" forState:UIControlStateNormal];
        sellableButton.layer.borderColor = themeColor.CGColor;
        sellableButton.layer.borderWidth = 1;
        sellableButton.backgroundColor = [UIColor colorFromHexCode:@"ffffff"];
        [sellableButton setTitleColor:themeColor forState:UIControlStateNormal];
    }
    else
    {
        sellableButton.layer.borderWidth = 0;
        sellableButton.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
        [sellableButton setTitleColor:[UIColor colorFromHexCode:@"b3b3b3"] forState:UIControlStateNormal];
        [sellableButton setTitle:@"未售卖" forState:UIControlStateNormal];
    }
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
